package com.example.study_manger.control;


import com.example.study_manger.entity.*;
import com.example.study_manger.repository.TaskRe;
import com.example.study_manger.repository.TrainingDocumentRe;
import com.example.study_manger.repository.TrainingPlanRe;
import com.example.study_manger.repository.UserTbRe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TaskController {
    @Autowired
    UserTbRe userTbRe;
    @Autowired
    private TaskRe taskRe;
    @Autowired
    private TrainingPlanRe trainingPlanRe;
    @Autowired
    private TrainingDocumentRe trainingDocumentRe;
    @PostMapping("/getTasks/{id}")
    public List<Task> getTasks(@PathVariable Long id){
        System.out.println(id);
        return taskRe.findByTrainingPlanId(id);
    }
    @PostMapping("/addTask/{id}")
    public List<Task> addTask(@PathVariable Long id, @RequestBody Task task){
        TrainingPlan trainingPlan=trainingPlanRe.findById(id).orElse(null);
        task.setTrainingPlan(trainingPlan);
        taskRe.save(task);
        System.out.println(taskRe.findByTrainingPlanId(id));
        return taskRe.findByTrainingPlanId(id);
    }
    @PostMapping("/deleteTask/{id}")
    public List<Task> deleteTask(@PathVariable Long id){
        Long tId= taskRe.findTrainPlanIdByTaskId(id);
        System.out.println(tId);
        taskRe.deleteById(id);
        return taskRe.findByTrainingPlanId(tId);
    }
    @GetMapping("/getResourceList/{username}")
    public List<TrainingDocument> getResourceList(@PathVariable String username){
        System.out.println("getResourceList");
        UserTb userTb = userTbRe.findByUsername(username);
        System.out.println(userTbRe.findByUsername(username));;
        if (userTb!=null){
            Role role = userTb.getRole();
            System.out.println(role);
            List<TrainingDocument> allByRole = trainingDocumentRe.findAllByRole_roleName("superAdmin");
            System.out.println(allByRole);
            return allByRole;
        }
       return null;
    }

}
